package xyz.jpcr.covid19app.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.text.NumberFormat;
import java.util.ArrayList;

import xyz.jpcr.covid19app.Model.Country.CountryResults;
import xyz.jpcr.covid19app.Activity.PerProvinceData;
import xyz.jpcr.covid19app.R;

public class ProvinceAdapter extends RecyclerView.Adapter<ProvinceAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<CountryResults> countryResultsArrayList;
    private ProvinceAdapter.OnItemClickListener mListener;
    public static final String PROVINCE_CURRENT_CONFIRMED = "currentConfirmedCount";
    public static final String PROVINCE_SUSPECTED = "suspectedCount";
    public static final String PROVINCE_CURED = "curedCount";
    public static final String PROVINCE_DEAD = "deadCount";
    public static final String PROVINCE_CONFIRMED = "confirmedCount";
    public static final String PROVINCE_LAST_UPDATE_TIME = "updateTime";
    public static final String PROVINCE_NAME = "provinceEnglishName";

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(ProvinceAdapter.OnItemClickListener listener) {
        mListener = listener;
    }

    public ProvinceAdapter(Context context, ArrayList<CountryResults> countryResultsArrayList) {
        mContext = context;
        this.countryResultsArrayList = countryResultsArrayList;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_province_wise_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CountryResults clickedItem =  countryResultsArrayList.get(position);
                Intent intent = new Intent(mContext, PerProvinceData.class);
                intent.putExtra(PROVINCE_NAME, clickedItem.getProvinceEnglishName());
                intent.putExtra(PROVINCE_CURRENT_CONFIRMED, clickedItem.getCurrentConfirmedCount());
                intent.putExtra(PROVINCE_SUSPECTED, clickedItem.getSuspectedCount());
                intent.putExtra(PROVINCE_CURED, clickedItem.getCuredCount());
                intent.putExtra(PROVINCE_DEAD, clickedItem.getDeadCount());
                intent.putExtra(PROVINCE_CONFIRMED, clickedItem.getConfirmedCount());
                intent.putExtra(PROVINCE_LAST_UPDATE_TIME, clickedItem.getUpdateTime());

                mContext.startActivity(intent);
            }
        });

        CountryResults currentItem = countryResultsArrayList.get(position);
        String provinceName = currentItem.getProvinceEnglishName();
        int totalConfirmed = currentItem.getConfirmedCount();
        holder.provinceName.setText(provinceName);
        holder.totalConfirmed.setText(NumberFormat.getInstance().format(totalConfirmed));
    }

    @Override
    public int getItemCount() {
        return countryResultsArrayList.size();
    }

    public void filterList(ArrayList<CountryResults> countryResultsArrayList) {
        this.countryResultsArrayList = countryResultsArrayList;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView provinceName, totalConfirmed;
        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            provinceName = itemView.findViewById(R.id.province_name_textview);
            totalConfirmed = itemView.findViewById(R.id.province_confirmed_textview);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        int position = getAdapterPosition();
                        if(position != RecyclerView.NO_POSITION) {
                            mListener.onItemClick(position);
                        }
                    }
                }
            });
        }
    }
}
